package getBatterySample;

/**
 * @projectName: code-snippet
 * @className: SampleBeen
 * @description:
 * @author: JLONG
 * @date: 2024/11/29 13:47
 * @version: 1.0
 */
public class SampleBeen {
    private String labelID;
    private String sampleValue;
    private double lowLoadVoltage;
    private double highLoadVoltage;
    private String result;
    public SampleBeen(String labelID, String sampleValue) {
        this.labelID = labelID;
        this.sampleValue = sampleValue;
        double[] loadVoltage = getLoadVoltage(sampleValue);
        this.lowLoadVoltage = loadVoltage[0];
        this.highLoadVoltage = loadVoltage[1];
        this.result = loadVoltage[0]>loadVoltage[1]?"OK":"NG";
    }
    private static double[] getLoadVoltage(String sampleValue){
        // 创建一个StringBuilder来存储二进制字符串
        StringBuilder binaryString = new StringBuilder();
        // 遍历每个字符
        for (char hexChar : sampleValue.toCharArray()) {
            // 将十六进制字符转换为整数
            int decimalValue = Integer.parseInt(Character.toString(hexChar), 16);
            // 将整数转换为4位的二进制字符串并追加到StringBuilder中
            binaryString.append(String.format("%4s", Integer.toBinaryString(decimalValue)).replace(' ', '0'));
        }
        double lowLoadVoltage = Integer.parseInt(binaryString.substring(0,10),2) * 3.75/1024;
        double highLoadVoltage = Integer.parseInt(binaryString.substring(10,20),2) * 3.75/1024;
        return new double[]{lowLoadVoltage,highLoadVoltage};
    }

    public String getLabelID() {
        return labelID;
    }

    public String getSampleValue() {
        return sampleValue;
    }

    public double getLowLoadVoltage() {
        return lowLoadVoltage;
    }

    public double getHighLoadVoltage() {
        return highLoadVoltage;
    }

    public String getResult() {
        return result;
    }
}
